package com.newtouch.bxzs.business.message.websocket.service;

import com.newtouch.bxzs.business.message.websocket.constant.Constant;
import com.newtouch.bxzs.common.base.model.bo.BroadcastMessageBO;
import com.newtouch.bxzs.common.base.model.bo.PushMessageBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/2 15:17</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class WebsocketService {
    @Autowired
    private SimpMessagingTemplate template;

    /**
     * 广播
     * 发给所有在线用户
     *
     * @param msg 消息
     */
    public void sendMsg(BroadcastMessageBO msg) {
        template.convertAndSend(Constant.PRODUCER_PATH, msg);
    }

    /**
     * 推送发送给指定用户
     * @param users 用户列表
     * @param msg 消息
     */
    public void send2Users(List<String> users, PushMessageBO msg) {
        users.forEach(userId -> template.convertAndSendToUser(userId, Constant.P2P_PUSHPATH, msg));
    }
}
